<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\HelpProblemInfoModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\utils\Helper;

class HelpProblemInfoService  extends BaseModelService
{
    public static function getModel()
    {
        return new HelpProblemInfoModel;
    }


    /**
     * 问题类型
     */
    public static function getTypeListInfo(){
        return [
            "DeviceSetting" => "Setting",
            "AccountSoft" => "Account",
            "Connection" => "Connection",
            "PlayBack" => "Replay",
            "DeviceManagent" => "Managent",
            "MoreProblem" => "More"
        ];
    }

    public static function typeNameList(){
        $data = self::getTypeListInfo();
        unset($data['MoreProblem']);
        $res = [];
        foreach($data as $key => $val){
            $res[] = [
                'type' => $key,
                'name' => $key,
            ];
        }
        return $res;
    }

    /**
     * 热门问题
     */
    public static function getHotBaseInfoList($param,$size = 10){
        $data = HelpProblemInfoModel::where(["language" => $param['language']])->order("total desc")->limit($size)->select();
        $data =  HelpProblemInfoModel::formatCollectionData($data);
        return Helper::listArrayColumn($data,['id','title']);
    }

    public static function getAllByType($param){
        $type = $param['type'];
        if(!in_array($type,array_values(self::getTypeListInfo()))){
            return [];
        }
        if($type === 'More'){
            unset($param['type']);
        }
        $data = self::getAllByFilter($param);
        return Helper::listArrayColumn($data,['id','title']);
    }

    public static function detail($id){
        $data = self::getBase($id);
        if(!$data){
            return null;
        }
        $description = $data['description'];
        if (strpos($description, ";") !== false) {
            $data["message"] = explode(";", $description);
        } else {
            $data["message"][] = $description;
        }
        return $data;
    }

    /**
     * 访问次数+1
     */
    public static function addVisit($id){
        HelpProblemInfoModel::where('id',$id)->setInc('total');
    }

    public static function addHelp($param){        
        $item  = Helper::arrayCamelKeyToUnderscore($param);
        self::add($item);
    }

    public static function updateHelp($param){        
        $id =  $param['id'];
        unset($param['id']);
        $origin = self::getBase($id);        
        if(empty($origin)){
            throw new ApiException("origin data is not exist",ErrnoPlus::COMMON_ERROR);
        }
        $item = Helper::arrayCamelKeyToUnderscore($param);
        $update = [];
        foreach($item as $key => $val){
            if($val != $origin[$key]){
                $update[$key] = $val;
            }
        }
        if(count($update) > 0){
            self::update($id,$update);
        }
    }

    public static function delHelp($id){
        self::del($id);
    }

    public static function buildCondition($filter)
    {
        return [];
    }

}
